﻿using System.Collections.Generic;

namespace Codemonk.Common.ObjectModel
{
    public class TransactionContext : ITransactionContext
    {
        public TransactionContext()
        {
            TransactionContextManager.Push(this);
        }

        private List<TransactionObject> objs;

        internal void Add(TransactionObject obj)
        {
            if (objs == null) objs = new List<TransactionObject>();
            if (!objs.Contains(obj)) objs.Add(obj);
        }

        public void Commit()
        {
            if (objs == null) return;
            foreach (var obj in objs) obj.Commit();
        }

        public int Count
        {
            get
            {
                if (objs == null) return 0;
                return objs.Count;
            }
        }

        public void Dispose()
        {
            TransactionContextManager.Pop();
        }
    }
}
